home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / IMGLIST.PAK / DRAWCEL.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  273 lines

  1. // ---------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
  4. // ---------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #if !defined(OWL_APPLICAT_H)
  7. # include <owl/applicat.h>
  8. #endif
  9. #if !defined(OWL_FRAMEWIN_H)
  10. # include <owl/framewin.h>
  11. #endif
  12. #if !defined(OWL_IMAGELST_H)
  13. # include <owl/imagelst.h>
  14. #endif
  15. #include <math.h>
  16. #include "drawcel.rh"
  17.  
  18. #if !defined(BI_PLAT_WIN32)
  19. # error This example must be built as a WIN32 target - It requires the 
  20. # error 'drag' support of IMAGELIST.
  21. #endif
  22.  
  23. // Few constants
  24. //
  25. const int ImageWidth  = 80;
  26. const int ImageHeight = 50;
  27. const int NumImages   = 2;
  28. const TColor MaskColor= RGB(255, 0, 0);
  29.  
  30. //
  31. // class TClientWindow
  32. // ~~~~~ ~~~~~~~~~~~~~
  33. class TClientWindow : public TWindow {
  34.   public:
  35.       TClientWindow(TWindow* parent= 0);
  36.      ~TClientWindow();
  37.  
  38.   protected:
  39.       void          SetupWindow();
  40.       void          Paint(TDC& dc, bool erase, TRect& rect);
  41.     void          EvSize(uint, TSize&);
  42.     void          EvLButtonDown(uint, TPoint& );
  43.     void          EvLButtonUp(uint, TPoint& );
  44.     void          EvMouseMove(uint, TPoint& );
  45.       void                  CmFlyImage();
  46.       void                  CmBirdImage();
  47.       void                  CmTurtleImage();
  48.  
  49.       TImageList*   ImageList;
  50.       TSize         ImgSize;
  51.     bool          Dragging;
  52.     int           Xpos;
  53.     int           Ypos;
  54.     int           Xoffset;
  55.     int           Yoffset;
  56.     int           CurImage;
  57.  
  58.   DECLARE_RESPONSE_TABLE(TClientWindow);
  59. };
  60.  
  61.  
  62. //
  63. //
  64. //
  65. DEFINE_RESPONSE_TABLE1(TClientWindow, TWindow)
  66.   EV_WM_SIZE,
  67.   EV_WM_LBUTTONDOWN,
  68.   EV_WM_LBUTTONUP,
  69.   EV_WM_MOUSEMOVE,
  70.     EV_COMMAND(CM_FLY, CmFlyImage),
  71.     EV_COMMAND(CM_BIRDIMAGE, CmBirdImage),
  72.     EV_COMMAND(CM_TURTLEIMAGE, CmTurtleImage),
  73. END_RESPONSE_TABLE;
  74.  
  75.  
  76. //
  77. //
  78. //
  79. TClientWindow::TClientWindow(TWindow* parent) 
  80.               :TWindow(parent), ImageList(0), CurImage(0), Dragging(false)
  81. {
  82.   ModifyStyle(0, WS_CLIPSIBLINGS|WS_CLIPCHILDREN);
  83.   Xpos =  Ypos = 0;
  84. }
  85.  
  86.  
  87. //
  88. //
  89. //
  90. TClientWindow::~TClientWindow()
  91. {
  92.   delete ImageList;
  93. }
  94.  
  95.  
  96. //
  97. //
  98. //
  99. void
  100. TClientWindow::SetupWindow()
  101. {
  102.   TWindow::SetupWindow();
  103.   ImageList = new TImageList(*GetApplication(), IDB_BMPSTRIP, ImageWidth, 1,
  104.                                                     MaskColor, IMAGE_BITMAP, ILC_COLOR);
  105. }
  106.  
  107.  
  108. //
  109. //
  110. //
  111. void
  112. TClientWindow::Paint(TDC& dc, bool, TRect&)
  113. {
  114.   TBitmap* skyBmp = new TBitmap(*GetApplication(), IDB_SKYBMP);
  115.   TPointer<TBitmap> __clnBmp = skyBmp;
  116.  
  117.   TMemoryDC memDc(dc);
  118.   memDc.SelectObject(*skyBmp);
  119.  
  120.   TRect rect;
  121.   GetClientRect(rect);
  122.   dc.StretchBlt(rect.left, rect.top, rect.right, rect.bottom, memDc, 0, 0,
  123.                           skyBmp->Width(), skyBmp->Height());
  124.  
  125.   if (Xpos + ImageWidth > rect.right)
  126.     Xpos= rect.right-ImageWidth;
  127.   if (Ypos + ImageHeight > rect.bottom)
  128.     Ypos= rect.bottom-ImageHeight;
  129.  
  130.   if (ImageList && !Dragging)
  131.     ImageList->Draw(CurImage, dc, Xpos, Ypos, ILD_NORMAL);
  132.  
  133.   memDc.RestoreBitmap();
  134. }
  135.  
  136. void
  137. TClientWindow::EvSize(uint sizeType, TSize& size)
  138. {
  139.   TWindow::EvSize(sizeType,size);
  140.   InvalidateRect(GetClientRect(),false);
  141. }
  142.  
  143. void
  144. TClientWindow::EvLButtonDown(uint modeKeys, TPoint& point)
  145. {
  146.   TRect imageRect(Xpos, Ypos, Xpos + ImageWidth, Ypos + ImageHeight);
  147.   bool hitTest = PtInRect(&imageRect, point); 
  148.   if (ImageList && hitTest) {
  149.  
  150.     // Offset of mouse down within image
  151.     //
  152.     Xoffset = point.x - Xpos;
  153.     Yoffset = point.y - Ypos;
  154.  
  155.     Dragging= true;
  156.     ShowCursor(FALSE); 
  157.     SetCapture();
  158.     InvalidateRect(imageRect, false);
  159.     UpdateWindow();
  160.     ImageList->SetDragCursorImage(CurImage, 0, 0);
  161.     ImageList->BeginDrag(CurImage, 0, 0);
  162.     ImageList->DragEnter(*this, Xpos, Ypos);
  163.   }
  164.   else
  165.     TWindow::EvLButtonDown(modeKeys, point);
  166. }
  167.  
  168. void
  169. TClientWindow::EvLButtonUp(uint modeKeys, TPoint& point)
  170. {
  171.   if (Dragging) {
  172.     Xpos= point.x - Xoffset;
  173.     Ypos= point.y - Yoffset;
  174.     ImageList->DragLeave(*this);
  175.     ImageList->EndDrag();
  176.     ReleaseCapture();
  177.     ShowCursor(TRUE); 
  178.     InvalidateRect(GetClientRect(), false);
  179.     Dragging= false;
  180.   }
  181.   else  
  182.     TWindow::EvLButtonUp(modeKeys, point);
  183. }
  184.  
  185. void
  186. TClientWindow::EvMouseMove(uint modeKeys, TPoint& point)
  187. {
  188.   if (Dragging)
  189.     ImageList->DragMove(point.x - Xoffset, point.y - Yoffset);
  190.   else
  191.     TWindow::EvMouseMove(modeKeys, point);
  192. }
  193.  
  194.  
  195. void
  196. TClientWindow::CmFlyImage()
  197. {
  198.   TRect rect;
  199.   GetClientRect(rect);
  200.   int mid = rect.bottom/2 - ImageHeight/2;
  201.  
  202.   Xpos = 0;
  203.   Ypos = mid;
  204.  
  205.   Dragging= true;
  206.   Invalidate(false);
  207.   UpdateWindow();
  208.   ShowCursor(FALSE); 
  209.   SetCapture();
  210.  
  211.   ImageList->SetDragCursorImage(CurImage, 0, 0);
  212.   ImageList->BeginDrag(CurImage, 0, 0);
  213.   ImageList->DragEnter(*this, Xpos, Ypos);
  214.   while (Xpos < (rect.right - ImageWidth)) {
  215.    Xpos++;
  216.    Ypos = mid + int(sin((Xpos*M_PI)/180) * mid);
  217.    ImageList->DragMove(Xpos, Ypos);
  218.   }
  219.  
  220.   ImageList->DragLeave(*this);
  221.   ImageList->EndDrag();
  222.   Dragging= false;
  223.  
  224.   ReleaseCapture();
  225.   ShowCursor(TRUE); 
  226.   Invalidate(false);
  227. }
  228.  
  229. void
  230. TClientWindow::CmBirdImage()
  231. {
  232.   CurImage=0;
  233.   InvalidateRect(GetClientRect(), false);
  234. }
  235.  
  236. void
  237. TClientWindow::CmTurtleImage()
  238. {
  239.   CurImage=1;
  240.   InvalidateRect(GetClientRect(), false);
  241. }
  242.  
  243. //
  244. // class TSampleApp
  245. // ~~~~~ ~~~~~~~~~~
  246. class TSampleApp : public TApplication {
  247.   public:
  248.      TSampleApp() : TApplication() {}
  249.      void InitMainWindow();
  250. };
  251.  
  252.  
  253. //
  254. //
  255. //
  256. void
  257. TSampleApp::InitMainWindow()
  258. {
  259.   TFrameWindow *frame = new TFrameWindow(0, "ImageList Drag", new TClientWindow());
  260.   frame->AssignMenu(IDM_IMAGEMENU);
  261.   SetMainWindow(frame);
  262. }
  263.  
  264.  
  265. //
  266. //
  267. //
  268. int
  269. OwlMain(int /*argc*/, char */*argv*/[])
  270. {
  271.   return TSampleApp().Run();
  272. }
  273.